/*
 * 文 件    名:  OrderProductModel.java
 * 创建日期:  2012-10-25上午11:49:46
 * 版          权:  Epro Technologies Co., Ltd. Copyright 2011-2012,  All rights reserved
 * 作          者:  df085
 */
package com.weipu.common.facade.model;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONObject;

import android.content.ContentValues;
import android.os.Parcel;
import android.os.Parcelable;

import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.content.colum.BrowsingHistoryColumn;
import com.weipu.common.facade.content.colum.MyCartColumn;

/** 
 * @Description 订单里的商品对象或者购物车里的商品对象或者历史浏览对象
 * @version [版本号, 2012-10-25]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author df085
 */
public class OrderProductModel implements Parcelable
{
    
	/**
    *id
    */
    private int id=-1;
    
    /**
     * 产品sku
     */
    private int sku=-1;
    
    /** 
     * 商品名称
     */
    private String shortHeadLine1="";
    
    /**
     * 价格
     */
    private float price = -1f;
    
    /** 
     * 订单商品数量
     */
    private int quantity;
    
    /** 
     * 图片url
     */
    private String smallPictureURL="";
    
    /** 
     * 中图片url
     */
    private String mediumPictureURL="";
    
    /** 
     * 大图片url
     */
    private String bigPictureURL="";
    
    /** 
     * 市场价
     */
    private float listprice;
    
    private int freeShipping;
    
    /** 
     * @Fields productRating : 商品平均评分
     */
    private float productRating;
    
    /** 
     * @Fields reviewCount : 商品的评论总数
     */
    private int reviewCount;
    
    private String isFavorite="0";
    
    private String  webUrl;
    
    /**
     * 单个优惠金额
     */
    private double discount;
    
    /**
     * 订购的数量
     */
    private int orderedQty;
    
    /**
     * 请求的数量
     */
    private int requestQty;
    
    /**
     * 赠送的数量
     */
    private int giftQty;
    
    private double lineDiscountTotal;//SKU优惠总金额 Discount * OrderQty
    private double  lineGoodsTotal;//正常售价小计 UnitPrice * OrderQty
    private double LineTotal;//购买价小计 Price * OrderQty
    
    private SalesLimit salesLimit;//库存。若为空表示不限量
    
    private boolean isGift = false;//是否是赠送sku的标记
    
    private String currencyCodeSymbol;//货币符号
    
    private String CurrencyCode;//货币简写
    
    
    
    public String getCurrencyCodeSymbol() {
		return currencyCodeSymbol;
	}

	public void setCurrencyCodeSymbol(String currencyCodeSymbol) {
		this.currencyCodeSymbol = currencyCodeSymbol;
	}

	public String getCurrencyCode() {
		return CurrencyCode;
	}

	public void setCurrencyCode(String currencyCode) {
		CurrencyCode = currencyCode;
	}

	public boolean isGift() {
		return isGift;
	}

	public void setGift(boolean isGift) {
		this.isGift = isGift;
	}

	public SalesLimit getSalesLimit() {
		return salesLimit;
	}

	public void setSalesLimit(SalesLimit salesLimit) {
		this.salesLimit = salesLimit;
	}

	public String getBigPictureURL() {
		return bigPictureURL;
	}

	public void setBigPictureURL(String bigPictureURL) {
		this.bigPictureURL = bigPictureURL;
	}

	public float getListprice() {
		return listprice;
	}

	public void setListprice(float listprice) {
		this.listprice = listprice;
	}

	public String getWebUrl() {
		return webUrl;
	}

	public void setWebUrl(String webUrl) {
		this.webUrl = webUrl;
	}

	public double getDiscount() {
		return discount;
	}

	public void setDiscount(double discount) {
		this.discount = discount;
	}

	public int getOrderedQty() {
		return orderedQty;
	}

	public void setOrderedQty(int orderedQty) {
		this.orderedQty = orderedQty;
	}
	
	public int getRequestQty() {
		return requestQty;
	}

	public void setRequestQty(int requestQty) {
		this.requestQty = requestQty;
	}

	public int getGiftQty() {
		return giftQty;
	}

	public void setGiftQty(int giftQty) {
		this.giftQty = giftQty;
	}

	public double getLineDiscountTotal() {
		return lineDiscountTotal;
	}

	public void setLineDiscountTotal(double lineDiscountTotal) {
		this.lineDiscountTotal = lineDiscountTotal;
	}

	public double getLineGoodsTotal() {
		return lineGoodsTotal;
	}

	public void setLineGoodsTotal(double lineGoodsTotal) {
		this.lineGoodsTotal = lineGoodsTotal;
	}

	public double getLineTotal() {
		return LineTotal;
	}

	public void setLineTotal(double lineTotal) {
		LineTotal = lineTotal;
	}

	public static Parcelable.Creator<OrderProductModel> getCreator() {
		return CREATOR;
	}

	public int getReviewCount()
    {
        return reviewCount;
    }
    
    public void setReviewCount(int reviewCount)
    {
        this.reviewCount = reviewCount;
    }
    
    public float getProductRating()
    {
        return productRating;
    }
    
    public void setProductRating(float productRating)
    {
        this.productRating = productRating;
    }
    
    public int getFreeShipping()
    {
        return freeShipping;
    }
    
    public void setFreeShipping(int freeShipping)
    {
        this.freeShipping = freeShipping;
    }
    
    public float getlistprice()
    {
        return listprice;
    }
    
    public void setlistprice(float listprice)
    {
        this.listprice = listprice;
    }
    
    public int getId()
    {
        return id;
    }
    
    public void setId(int id)
    {
        this.id = id;
    }
    
    public int getSku()
    {
        return sku;
    }
    
    public void setSku(int sku)
    {
        this.sku = sku;
    }
    
    public String getShortHeadLine1()
    {
        return shortHeadLine1;
    }
    
    public void setShortHeadLine1(String shortHeadLine1)
    {
        this.shortHeadLine1 = shortHeadLine1;
    }
    
    public float getPrice()
    {
        return price;
    }
    
    public void setPrice(float price)
    {
        this.price = price;
    }
    
    public int getQuantity()
    {
        return quantity;
    }
    
    public void setQuantity(int quantity)
    {
        this.quantity = quantity;
    }
    
    public String getSmallPictureURL()
    {
        return smallPictureURL;
    }
    
    public void setSmallPictureURL(String smallPictureURL)
    {
        this.smallPictureURL = smallPictureURL;
    }
    
    
    
    public String getMediumPictureURL() {
		return mediumPictureURL;
	}

	public void setMediumPictureURL(String mediumPictureURL) {
		this.mediumPictureURL = mediumPictureURL;
	}

	public String getIsFavorite()
    {
        return isFavorite;
    }
    
    public void setIsFavorite(String isFavorite)
    {
        this.isFavorite = isFavorite;
    }

    @Override
    public String toString() {
        return "OrderProductModel{" +
                "id=" + id +
                ", sku=" + sku +
                ", shortHeadLine1='" + shortHeadLine1 + '\'' +
                ", price=" + price +
                ", quantity=" + quantity +
                ", smallPictureURL='" + smallPictureURL + '\'' +
                ", mediumPictureURL='" + mediumPictureURL + '\'' +
                ", bigPictureURL='" + bigPictureURL + '\'' +
                ", listprice=" + listprice +
                ", freeShipping=" + freeShipping +
                ", productRating=" + productRating +
                ", reviewCount=" + reviewCount +
                ", isFavorite='" + isFavorite + '\'' +
                ", webUrl='" + webUrl + '\'' +
                ", discount=" + discount +
                ", orderedQty=" + orderedQty +
                ", requestQty=" + requestQty +
                ", giftQty=" + giftQty +
                ", lineDiscountTotal=" + lineDiscountTotal +
                ", lineGoodsTotal=" + lineGoodsTotal +
                ", LineTotal=" + LineTotal +
                ", salesLimit=" + salesLimit +
                ", isGift=" + isGift +
                ", currencyCodeSymbol='" + currencyCodeSymbol + '\'' +
                ", CurrencyCode='" + CurrencyCode + '\'' +
                '}';
    }

    /**
     * @author epro
     * 库存对象
     */
    public class SalesLimit{
    	//库存数量
        int salesLimitQty;
        //DistributionPolicy有三个值，0-NoShipping-不发货，1-SalesLimit-限量销售，2-UnSalesLimit-不限量
        int distributionPolicy;
        //3种库存满足的情况
        //1.SalesLimit=null，不限量
        //2.SalesLimit.DistributionPolicy=UnSalesLimit，不限量
        //3.SalesLimit.DistributionPolicy=SalesLimit，限量，的时候， SalesLimitQty大于购买数量
        //反之，2种不满足库存的情况
        //1.SalesLimit.DistributionPolicy=NoShipping，不发货
        //2.SalesLimit.DistributionPolicy=SalesLimit，限量，的时候， SalesLimitQty小于购买数量
		public int getSalesLimitQty() {
			return salesLimitQty;
		}
		public void setSalesLimitQty(int salesLimitQty) {
			this.salesLimitQty = salesLimitQty;
		}
		public int getDistributionPolicy() {
			return distributionPolicy;
		}
		public void setDistributionPolicy(int distributionPolicy) {
			this.distributionPolicy = distributionPolicy;
		}
    }
    
    /**
     * 对象转为map（购物车）
     * @return [参数说明]
     * @return ContentValues [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public ContentValues template2map()
    {
        ContentValues map = new ContentValues();
        if (-1 != id)
        {
            map.put(MyCartColumn._ID, id);
        }
        if (-1 != sku)
        {
            map.put(MyCartColumn.SKU, sku);
        }
        
        if (-1 != quantity)
        {
            map.put(MyCartColumn.QUANTITY, quantity);
        }
        if (-1 != freeShipping)
        {
            map.put(MyCartColumn.FREESHIPPING, freeShipping);
        }
        
        if (null != shortHeadLine1)
        {
            map.put(MyCartColumn.SHORTHEADLINE1, shortHeadLine1);
        }
        if (-1 != price)
        {
            map.put(MyCartColumn.PRICE, price);
        }
        if (null != smallPictureURL)
        {
            map.put(MyCartColumn.SMALLPICTUREURL, smallPictureURL);
        }
        if(null!=mediumPictureURL){
        	map.put(MyCartColumn.MEDIUMPICTUREURL, mediumPictureURL);
        }
        if (-1 != listprice)
        {
            map.put(MyCartColumn.LISTPRICE, listprice);
        }
//        if (null != isFavorite)
//        {
//            map.put(MyCartColumn.ISFAVORITE, listprice);
//        }
        return map;
        
    }
    
    /**
     * 对象转为map(历史）
     * @return [参数说明]
     * @return ContentValues [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public ContentValues template3map()
    {
        //插入数据库时用到
        ContentValues map = new ContentValues();
        
        if (-1 != id)
        {
            map.put(BrowsingHistoryColumn._ID, id);
        }
        if (-1 != sku)
        {
            map.put(BrowsingHistoryColumn.SKU, sku);
        }
        
        if (null != shortHeadLine1)
        {
            map.put(BrowsingHistoryColumn.SHORTHEADLINE1, shortHeadLine1);
        }
        
        if (null != smallPictureURL)
        {
            map.put(BrowsingHistoryColumn.SMALLPICTUREURL, smallPictureURL);
        }
        
        if (null != smallPictureURL)
        {
            map.put(BrowsingHistoryColumn.MEDIUMPICTUREURL, mediumPictureURL);
        }
        
        if (-1 != price)
        {
            map.put(BrowsingHistoryColumn.PRICE, price);
        }
        if (-1 != listprice)
        {
            map.put(BrowsingHistoryColumn.OLDPRICE, listprice);
        }
        if (-1 != reviewCount)
        {
            map.put(BrowsingHistoryColumn.REVIEWCOUNT, reviewCount);
        }
        if (-1 != productRating)
        {
            map.put(BrowsingHistoryColumn.PRODUCTRATING, productRating);
        }
        return map;
    }
    
    /**
     * 把wishModels转换成productmodels
     * @param wishModels
     * @return  [参数说明]
     * @return ArrayList<ProductModel> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static ArrayList<ProductModel> orderProductModels2pModels(ArrayList<OrderProductModel> orderProductModels)
    {
        ArrayList<ProductModel> productModels = new ArrayList<ProductModel>();
        for (int i = 0; i < orderProductModels.size(); i++)
        {
            productModels.add(orderProductModel2pModel(orderProductModels.get(i)));
        }
        return productModels;
    }
    
    /**
      * 把wishModel转换成productmodel
      * @param orderProductModel
      * @return  [参数说明]
      * @return ProductModel [返回类型说明]
      * @exception throws [违例类型] [违例说明]
      * @see [类、类#方法、类#成员]
      */
    public static ProductModel orderProductModel2pModel(OrderProductModel orderProductModel)
    {
        ProductModel productModel = new ProductModel();
        productModel.setSku(orderProductModel.getSku());
        productModel.setShortHeadLine1(orderProductModel.getShortHeadLine1());
        productModel.setPrice(orderProductModel.getPrice());
        productModel.setListprice(orderProductModel.getlistprice());
        String[] pics = {orderProductModel.getSmallPictureURL()};
        productModel.setSmallPictureURL(pics);
        String[] picMediums = {orderProductModel.getMediumPictureURL()};
        productModel.setMediumPictureURL(picMediums);
        return productModel;
    }

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel parcel, int arg1) {
		parcel.writeInt(id);
		parcel.writeInt(sku);
		parcel.writeString(shortHeadLine1);
		parcel.writeFloat(price);
		parcel.writeInt(quantity);
		parcel.writeString(smallPictureURL);
		parcel.writeString(mediumPictureURL);
		parcel.writeString(bigPictureURL);
		parcel.writeFloat(listprice);
		parcel.writeInt(freeShipping);
		parcel.writeFloat(productRating);
		parcel.writeInt(reviewCount);
		parcel.writeString(isFavorite);
		parcel.writeDouble(lineDiscountTotal);
		parcel.writeDouble(lineGoodsTotal);
		parcel.writeDouble(LineTotal);
		parcel.writeString(webUrl);
	}
	
	 public static final Parcelable.Creator<OrderProductModel> CREATOR = new Creator<OrderProductModel>() {
		  
		  @Override
		  public OrderProductModel[] newArray(int size) {
		   // TODO Auto-generated method stub
		   return new OrderProductModel[size];
		  }
		  
		  @Override
		  public OrderProductModel createFromParcel(Parcel source) {
		   // TODO Auto-generated method stub
		   return new OrderProductModel(source);
		  }
		 };
		 
    @SuppressWarnings("unchecked")
    public OrderProductModel(Parcel source){
    	id=source.readInt();
    	sku=source.readInt();
    	shortHeadLine1=source.readString();
    	price=source.readFloat();
    	quantity=source.readInt();
    	smallPictureURL=source.readString();
    	mediumPictureURL=source.readString();
    	bigPictureURL = source.readString();
    	listprice=source.readFloat();
    	freeShipping=source.readInt();
    	productRating=source.readFloat();
    	reviewCount=source.readInt();
    	isFavorite=source.readString();
    	lineDiscountTotal = source.readDouble();
    	lineGoodsTotal = source.readDouble();
    	LineTotal = source.readDouble();
    	webUrl = source.readString();
    }
    
    public OrderProductModel(){
    	
    }
    
    /**
     * 将JSON对象转换为JavaBean的方法
     * @return
     */
    public  OrderProductModel json2OrderProductmModel(JSONObject data){
    	OrderProductModel orderProductModel = null;
    	if(data!=null){
    		orderProductModel = new OrderProductModel();
    		orderProductModel.setBigPictureURL((data.optJSONArray(ServerConstant.OrderProductModelField.OrderProductModelField_BigPictureURL)).optString(0));
    		orderProductModel.setDiscount(data.optDouble(ServerConstant.OrderProductModelField.OrderProductModelField_Discount));
    		orderProductModel.setFreeShipping(data.optInt(ServerConstant.OrderProductModelField.OrderProductModelField_FreeShipping));
    		orderProductModel.setGiftQty(data.optInt(ServerConstant.OrderProductModelField.OrderProductModelField_GiftQty));
    		orderProductModel.setLineDiscountTotal(data.optDouble(ServerConstant.OrderProductModelField.OrderProductModelField_LineDiscountTotal));
    		orderProductModel.setLineGoodsTotal(data.optDouble(ServerConstant.OrderProductModelField.OrderProductModelField_LineGoodsTotal));
    		orderProductModel.setLineTotal(data.optDouble(ServerConstant.OrderProductModelField.OrderProductModelField_LineTotal));
    		orderProductModel.setListprice((float)data.optDouble(ServerConstant.OrderProductModelField.OrderProductModelField_ListPrice));
    		orderProductModel.setMediumPictureURL((data.optJSONArray(ServerConstant.OrderProductModelField.OrderProductModelField_MediumPictureURL)).optString(0));
    		orderProductModel.setQuantity(data.optInt(ServerConstant.OrderProductModelField.OrderProductModelField_RequestQty));
    		orderProductModel.setOrderedQty(data.optInt(ServerConstant.OrderProductModelField.OrderProductModelField_OrderedQty));
    		orderProductModel.setRequestQty(data.optInt(ServerConstant.OrderProductModelField.OrderProductModelField_RequestQty));
    		orderProductModel.setPrice((float)data.optDouble(ServerConstant.OrderProductModelField.OrderProductModelField_Price));
    		orderProductModel.setProductRating((float)data.optDouble(ServerConstant.OrderProductModelField.OrderProductModelField_ProductRating));
    		orderProductModel.setReviewCount(data.optInt(ServerConstant.OrderProductModelField.OrderProductModelField_ReviewCount));
    		JSONObject salseLimit = data.optJSONObject(ServerConstant.OrderProductModelField.OrderProductModelField_SalesLimit);
    		if(salseLimit!=null){
    			SalesLimit limit = new OrderProductModel.SalesLimit();
    			limit.setSalesLimitQty(salseLimit.optInt("SalesLimitQty"));
    			limit.setDistributionPolicy(salseLimit.optInt("DistributionPolicy"));
    			orderProductModel.setSalesLimit(limit);
    		}
    		orderProductModel.setShortHeadLine1(data.optString(ServerConstant.OrderProductModelField.OrderProductModelField_ShortHeadLine));
    		orderProductModel.setSku(data.optInt(ServerConstant.OrderProductModelField.OrderProductModelField_Sku));
    		orderProductModel.setSmallPictureURL((data.optJSONArray(ServerConstant.OrderProductModelField.OrderProductModelField_SmallPictureURL)).optString(0));
    		orderProductModel.setWebUrl(data.optString(ServerConstant.OrderProductModelField.OrderProductModelField_WebUrl));
		}
    	return orderProductModel;
    }
}
